package cn.xfdzx.android.apps.shop.presenter;

import android.util.Log;

import java.util.List;
import java.util.Map;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.AfterMarketInfoBean;
import cn.xfdzx.android.apps.shop.bean.AfterSalesListRetailBean;
import cn.xfdzx.android.apps.shop.bean.AfterServiceUpLoadMoreImgBean;
import cn.xfdzx.android.apps.shop.bean.AfterServiceUpLoadSingImgBean;
import cn.xfdzx.android.apps.shop.bean.OrderDetailInfoBeen;
import cn.xfdzx.android.apps.shop.bean.OrderProcessBeen;
import cn.xfdzx.android.apps.shop.bean.ReturnReasonBean;
import cn.xfdzx.android.apps.shop.contract.AfterServiceContract;
import cn.xfdzx.android.apps.shop.model.AfterServiceModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import cn.xfdzx.android.apps.shop.util.TimeUtil;
import cn.xfdzx.android.apps.shop.util.Utils;
import io.reactivex.functions.Consumer;
import okhttp3.MultipartBody;

/**
 * @author zf
 * @date 2020/7/14.
 * Description：售后服务
 */
public class AfterServicePresenter extends BasePresenter<AfterServiceContract.View> implements AfterServiceContract.Presenter {

    private AfterServiceContract.Model model;

    public AfterServicePresenter() {
        model = new AfterServiceModel();
    }


    @Override
    public void uploadMoreImg( List<MultipartBody.Part> files) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.uploadMoreImg(files)
                .compose(RxScheduler.<AfterServiceUpLoadMoreImgBean>Flo_io_main())
                .as(mView.<AfterServiceUpLoadMoreImgBean>bindAutoDispose())
                .subscribe(new Consumer<AfterServiceUpLoadMoreImgBean>() {
                    @Override
                    public void accept(AfterServiceUpLoadMoreImgBean bean) throws Exception {
                        mView.onUploadMoreImgSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void uploadSingImg(List<MultipartBody.Part> files) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.uploadSingImg(files)
                .compose(RxScheduler.<AfterServiceUpLoadSingImgBean>Flo_io_main())
                .as(mView.<AfterServiceUpLoadSingImgBean>bindAutoDispose())
                .subscribe(new Consumer<AfterServiceUpLoadSingImgBean>() {
                    @Override
                    public void accept(AfterServiceUpLoadSingImgBean bean) throws Exception {
                        mView.onUploadSingImgSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getReturnReason() {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getReturnReason()
                .compose(RxScheduler.<ReturnReasonBean>Flo_io_main())
                .as(mView.<ReturnReasonBean>bindAutoDispose())
                .subscribe(new Consumer<ReturnReasonBean>() {
                    @Override
                    public void accept(ReturnReasonBean bean) throws Exception {
                        mView.getReturnReasonSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getAfterServiceRequest(Map<String, Object> param) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getAfterServiceRequest(param)
                .compose(RxScheduler.<OrderProcessBeen>Flo_io_main())
                .as(mView.<OrderProcessBeen>bindAutoDispose())
                .subscribe(new Consumer<OrderProcessBeen>() {
                    @Override
                    public void accept(OrderProcessBeen bean) throws Exception {
                        mView.getAfterServiceRequestSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getAfterServiceCancle(Map<String, Object> param) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getAfterServiceCancle(param)
                .compose(RxScheduler.<ReturnReasonBean>Flo_io_main())
                .as(mView.<ReturnReasonBean>bindAutoDispose())
                .subscribe(new Consumer<ReturnReasonBean>() {
                    @Override
                    public void accept(ReturnReasonBean bean) throws Exception {
                        mView.getAfterServiceCancleSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getAfterServiceProgress(Map<String, Object> param) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getAfterServiceProgress(param)
                .compose(RxScheduler.<OrderProcessBeen>Flo_io_main())
                .as(mView.<OrderProcessBeen>bindAutoDispose())
                .subscribe(new Consumer<OrderProcessBeen>() {
                    @Override
                    public void accept(OrderProcessBeen bean) throws Exception {
                        mView.getAfterServiceProgressSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getOrderDetail(String orderid) {
//View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getOrderDetail(orderid)
                .compose(RxScheduler.<OrderDetailInfoBeen>Flo_io_main())
                .as(mView.<OrderDetailInfoBeen>bindAutoDispose())
                .subscribe(new Consumer<OrderDetailInfoBeen>() {
                    @Override
                    public void accept(OrderDetailInfoBeen bean) throws Exception {
                        mView.getOrderDetailSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getMarketAfterServiceRequest(Map<String, Object> param) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getMarketAfterServiceRequest(param)
                .compose(RxScheduler.<OrderProcessBeen>Flo_io_main())
                .as(mView.<OrderProcessBeen>bindAutoDispose())
                .subscribe(new Consumer<OrderProcessBeen>() {
                    @Override
                    public void accept(OrderProcessBeen bean) throws Exception {
                        mView.getMarketAfterServiceRequestSucces(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getMarketAfterServiceCancle(Map<String, Object> param) {
//View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getMarketAfterServiceCancle(param)
                .compose(RxScheduler.<ReturnReasonBean>Flo_io_main())
                .as(mView.<ReturnReasonBean>bindAutoDispose())
                .subscribe(new Consumer<ReturnReasonBean>() {
                    @Override
                    public void accept(ReturnReasonBean bean) throws Exception {
                        mView.getMarketAfterServiceCancleSucces(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getMarketAfterServiceInfo(Map<String, Object> param) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getMarketAfterServiceInfo(param)
                .compose(RxScheduler.<AfterMarketInfoBean>Flo_io_main())
                .as(mView.<AfterMarketInfoBean>bindAutoDispose())
                .subscribe(new Consumer<AfterMarketInfoBean>() {
                    @Override
                    public void accept(AfterMarketInfoBean bean) throws Exception {
                        mView.getMarketAfterServiceInfoSucces(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }
}
